using System;
using System.Collections.Generic;

using Gtk;


/*
 * 
 * 1) Dictionary et clée unique. Problème dans l'affichage des albums.
 * Dans la méthode WriteAlbums() du controleur_collection, j'enlève les noms en doublon.
 * Je dois prendre en compte l'affichage de toutes les musiques des albums conserné et pas du premier.
 * 
 * 
 * 
 * 
 * 
 * 
 * 
*/

namespace Client
{
	public partial class MainWindow : Gtk.Window
	{
		private Controleur_Collection Ctrl_coll;
		private Gtk.TreeStore AlbumListStore;
		private Gtk.TreeStore MusicListStore;
		
		public MainWindow () : base(Gtk.WindowType.Toplevel) {
			Build ();
			
			
			// Album
			CellRendererToggle crTogglererAlbums = new CellRendererToggle();
			crTogglererAlbums.Activatable = true;
			crTogglererAlbums.Toggled += crt_toggledAlbum;
			
			Album.AppendColumn ("Selected", crTogglererAlbums, "active", 0);
			Album.AppendColumn ("Album",new CellRendererText(), "text", 1);
			Album.NodeSelection.Changed += new System.EventHandler (OnSelectionChangedAlbum);
			
			
			
			
			// Music
			CellRendererToggle crTogglererMusic = new CellRendererToggle();
			crTogglererMusic.Activatable = true;
			crTogglererMusic.Toggled += crt_toggledMusic;
			
			Music.AppendColumn ("Selected", crTogglererMusic, "active", 0);
			Music.AppendColumn ("Titre", new CellRendererText(), "text", 1);
		}
		
		
		public void AddControlleur(Controleur_Collection ctrl_coll) {
			Ctrl_coll = ctrl_coll;
		}
		
		
		private void crt_toggledAlbum(object o, ToggledArgs args) {
			TreeIter iter;

			if (AlbumListStore.GetIter (out iter, new TreePath(args.Path))) {
				bool old = (bool) AlbumListStore.GetValue(iter,0);
				AlbumListStore.SetValue(iter,0,!old);
				
				string album = AlbumListStore.GetValue(iter,1).ToString();
				Ctrl_coll.SelectSwitchedAlbum(album);
				Ctrl_coll.WriteMusicTree(album);
			}
		}
		
		private void crt_toggledMusic(object o, ToggledArgs args) {
			TreeIter iter;

			if (MusicListStore.GetIter (out iter, new TreePath(args.Path))) {
				TreeIter iterAlbum;
				AlbumListStore.GetIter (out iterAlbum, new TreePath(args.Path));
				
				
				
				bool old = (bool) MusicListStore.GetValue(iter,0);
				MusicListStore.SetValue(iter,0,!old);
				
				string music = MusicListStore.GetValue(iter,1).ToString();
				string album = AlbumListStore.GetValue(iterAlbum,1).ToString();
				
				Ctrl_coll.SelectSwitchedMusic(music);
				Ctrl_coll.WriteMainTree();
				Ctrl_coll.WriteMusicTree(album);
				
				//Music.Selection.SelectPath(iterAlbum);
			}
		}
		
		void OnSelectionChangedAlbum (object o, System.EventArgs args) {
			TreeSelection selection = Album.Selection;
			
			TreeModel model;
			TreeIter iter;
			
			// The iter will point to the selected row
			if(selection.GetSelected(out model, out iter))
				Ctrl_coll.WriteMusicTree(Album.Model.GetValue(iter, 1).ToString());
		}
		
		
		protected virtual void OnScanActionActivated (object sender, System.EventArgs e) {
			Gtk.FileChooserDialog fc=
				new Gtk.FileChooserDialog("Choose the folder to scan",
				                            this,
				                            FileChooserAction.SelectFolder,
				                            "Cancel",ResponseType.Cancel,
				                            "OK",ResponseType.Accept);
	
			if (fc.Run() == (int)ResponseType.Accept)
				Ctrl_coll.Scan(fc.Filename);
			
			fc.Destroy();
		}
		
		public void DisplayMain(Dictionary<string, bool> albums) {
			AlbumListStore = new Gtk.TreeStore (typeof (bool), typeof (string));
			
			foreach(KeyValuePair<string, bool> pair in albums)
				AlbumListStore.AppendValues(pair.Value, pair.Key);
			                              
			 Gtk.Application.Invoke (delegate {
				// Assign the model to the TreeView
				Album.Model = AlbumListStore;
			});
			
			                              
		}
		
		public void DisplayMusic(Dictionary<string, bool> musics) {
			MusicListStore = new Gtk.TreeStore (typeof (bool), typeof (string));
			
			foreach(KeyValuePair<string, bool> pair in musics)
				MusicListStore.AppendValues(pair.Value, pair.Key);
			
			 Gtk.Application.Invoke (delegate {
				// Assign the model to the TreeView
				Music.Model = MusicListStore;
			});
		}
		
	
		protected void OnDeleteEvent (object sender, DeleteEventArgs a) {
			Application.Quit ();
			a.RetVal = true;
		}
	}
}